package br.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.connection.ConnectionFactory;
import br.com.controller.HorarioController;

@WebServlet("/HorarioServlet")
public class HorarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public HorarioServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		try{
			doInser(request, response);
		}catch (Exception e){
			out.println("ocorreu algum erro");
		}

	}

	protected void doInser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		request.setCharacterEncoding("UTF-8");        
		response.setCharacterEncoding("UTF-8");
		String esc  = request.getParameter("escola");
		String cpf  = request.getParameter("cpf");
		int mat  = Integer.parseInt(request.getParameter("mat"));
		int fls  = Integer.parseInt(request.getParameter("fol"));
		String cont  = request.getParameter("cont");
		String h1  = request.getParameter("hEnt1");
		String s1  = request.getParameter("hSa1");
		String h2  = request.getParameter("hEnt2");
		String s2  = request.getParameter("hSa2");
		String obs  = request.getParameter("obs");

		Connection conn = null;

		try {

			conn = new ConnectionFactory().getConnection();
			HorarioController.inserir(conn, esc, cpf, mat, fls, cont, h1, s1, h2, s2, obs);

			out.print("<script type=\"text/javascript\">	alert(\"Cadastrado com sucesso.\");</script>");
			//out.print("<meta http-equiv='refresh' content='0;url=/WebTecEscola/Admin.jsp'>");
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (Exception ex) {ex.printStackTrace();}
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {}
		} 
	}

}
